import pygame, sys, time, os
from pygame.locals import *

# Init PyGame/Joysticks
pygame.init()
pygame.joystick.init()
joystick = pygame.joystick.Joystick(0)
joystick.init()

# Vars
interval = 0.01
iAS=dict({'0':0})	# Axis states
iBS=dict({'0':0})	# Button states
joystick_count = pygame.joystick.get_count()
print("joystick_count: "+str(joystick_count))
iAxes = joystick.get_numaxes()
iButtons = joystick.get_numbuttons()
for i in range(0,iAxes): iAS[i]=0
for i in range(0,iButtons): iBS[i]=0
loopQuit = False

# Display banner
os.clear
print("Joystick Testing Script\n-----------------------")

# Handler loop
while loopQuit == False:
	# Poll axes
	for i in range(0,iAxes):
		iState = joystick.get_axis(i)
		if(iAS[i] <> iState):
			if(i<>25):
				print("Axis #"+str(i)+" altered from ["+str(iAS[i])+"] to ["+str(iState)+"]")
				iAS[i]=iState
	
	# Poll buttons
	for i in range(0,iButtons):
		iState = joystick.get_button(i)
		if(iBS[i] <> iState):
			print("Button #"+str(i)+" altered from ["+str(iBS[i])+"] to ["+str(iState)+"]")
			iBS[i]=iState
    	
	# Event handler	
	for event in pygame.event.get():
		if event.type == QUIT: loopQuit = True
    	time.sleep(interval)

# Cleanup & exit
pygame.quit()
sys.exit()
